package com.lovo.roomProject.room.frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;

import javax.swing.*;

import com.lovo.netCRM.component.*;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.RoomDO;
import com.lovo.roomProject.pojo.RoomSetDO;
import com.lovo.roomProject.room.dao.RoomDAO;
import com.lovo.roomProject.room.dao.impl.RoomDAOImpl;
import com.lovo.roomProject.room.service.RoomService;
import com.lovo.roomProject.room.service.RoomSetService;
import com.lovo.roomProject.room.service.impl.RoomServiceImpl;
import com.lovo.roomProject.room.service.impl.RoomSetServiceImpl;
import com.lovo.roomProject.system.service.UserService;

/**
 * 宿舍管理系统
 *
 * @author 张成峰
 * @version 1.0
 * @description 房间设施对话框
 * @see
 */
public class RoomSetDialog extends JDialog {
    RoomSetService roomSetService = new RoomSetServiceImpl();
    RoomService roomService = new RoomServiceImpl();
    private LovoCutPage lovoCutPage = new LovoCutPage(50, 290, this) {
        @Override
        public void clickEvent(int current) {
            findRoomSet(current);

        }
    };


    /**
     * 房间主面板
     */
    private RoomPanel roomPanel;
    /**
     * 设施表格
     */
    private LovoTable roomSetTable;
    /**
     * 损坏描述文本域
     */
    private LovoTxtArea descriptionTxt = new LovoTxtArea("损坏描述", 80, 340, 200, 100, this);
    /**
     * 房间Id
     */
    private int roomId;

    public RoomSetDialog(JFrame jf, int roomId, RoomPanel roomPanel) {
        super(jf, true);
        this.roomId = roomId;
        this.roomPanel = roomPanel;

        this.setLayout(null);

        this.init();

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) screensize.getWidth() / 2 - 260;
        int y = (int) screensize.getHeight() / 2 - 300;
        this.setBounds(x, y, 520, 600);
        this.setVisible(true);
    }

    /**
     * 初始化
     */
    private void init() {
        LovoTitleLabel titleLabel = new LovoTitleLabel("房间设施信息", this);
        titleLabel.setLocation(180, titleLabel.getY());
        this.initTable();
        this.initData(roomId);

        LovoButton addButton = new LovoButton(40, 470, "添加损坏记录", "image/add.png", this);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                addRoomSet(roomId);
            }
        });

        LovoButton pressButton = new LovoButton(190, 470, "处理损坏设施", "image/update.png", this);
        pressButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int key = roomSetTable.getKeyByInt();
                if (key == -1) {
                    JOptionPane.showMessageDialog(null, "请选择行");
                    return;
                }
                int confirm = JOptionPane.showConfirmDialog(null, "已处理该项设施？");
                if (confirm == 0) {
                    pressRoomSet(key);
                }

            }
        });

        LovoButton cancelButton = new LovoButton(340, 470, "退出", "image/cancel.png", this);
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                RoomSetDialog.this.dispose();
            }
        });
    }

    //-------------------------

    /**
     * 初始化表格
     */
    private void initTable() {
        roomSetTable = new LovoTable(this,
                new String[]{"损坏描述", "报损日期", "是否解决"},
                new String[]{"description", "errorDate", "isPressed"},//学生实体属性名数组 new String[]{"description","errorDate"}
                "id");//主键属性名 setId
        roomSetTable.setSizeAndLocation(40, 120, 440, 200);
    }

    /**
     * 初始化数据
     *
     * @param roomId 房间ID
     */
    private void initData(int roomId) {
        findRoomSet(1);
    }

    /**
     * 添加损坏记录
     *
     * @param roomId 房间ID
     */
    private void addRoomSet(int roomId) {
        //验证数据，数据验证失败返回
        if (descriptionTxt.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "描述不能为空");
            return;
        }
        RoomDO roomDO = roomService.getById(roomId);
        RoomSetDO roomSetDO = new RoomSetDO();
        RoomDAO roomDAO = new RoomDAOImpl();
        roomSetDO.setRoomId(roomId);
        roomSetDO.setDescription(descriptionTxt.getText());
        roomSetDO.setErrorDate(LocalDate.now());
        //更新状态
        roomDO = roomDAO.getById(roomSetDO.getRoomId());
        roomDO.setStatus("设施损坏");
        roomDAO.update(roomDO);
        roomSetDO.setIsPressed("未处理");
        roomSetService.insert(roomSetDO);

        initData(roomId);
        roomPanel.initData();
    }

    /**
     * 处理损坏设施
     *
     * @param roomSetId 损坏设施ID
     */
    private void pressRoomSet(int roomSetId) {
        RoomDO roomDO = roomService.getById(roomSetId);
        RoomSetDO roomSetDO = roomSetService.getRoomSetById(roomSetId);
        roomSetDO.setIsPressed("已处理");
        if (roomSetDO.getDescription().equals("")) {
            roomDO.setStatus("正常");
        }
        roomSetService.update(roomSetDO);
        initData(roomId);
        roomPanel.initData();
    }


    /**
     * 查看房间设施
     */
    private void findRoomSet(int current) {

        CutPageBean cutPageBean = roomSetService.listRoomsSetByPage(current, this.roomId);
        roomSetTable.updateLovoTable(cutPageBean.getList());
        if (current == 1) {
            lovoCutPage.setTotalPage(cutPageBean.getTotalPage());
        }

    }

}
